// pages/open/open.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
      date1: '',
      date2: '',
      photo:'../assets/img/upimg.jpg',
      pics:'../assets/img/upimg.jpg',
      coverImg:'',
      shareImg:''
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      
    },
    //  点击开始日期组件确定事件
    bindDateStartChange: function (e) {
      var that = this;
      that.setData({
        date1: e.detail.value
      })
    },
  
    //  点击结束日期组件确定事件
    bindDateEndChange: function (e) {
      var that = this;
      that.setData({
        date2: e.detail.value
      })
    },
    sharechoose: function (e) {
      var that = this;
        wx.chooseImage({
              count: 1,
              sizeType: ['original', 'compressed'],
              sourceType: ['album', 'camera'],
              success (res) {
                console.log(res)
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var imgbox = res.tempFilePaths;
                that.setData({
                  pics:imgbox
                }); 
                wx.uploadFile({
                  url: 'https://zb.10soo.cn/wxmini/live/uploads',
                  method:'POST',
                  filePath:imgbox[0],//图片路径需要字符串而不是数组
                  name:'files',
                  formData: {
                    'user': 'test'
                  },
                  header: {
                    "Content-Type": "multipart/form-data"
                  },
                  success (res) {
                    var data = JSON.parse(res.data)
                    that.setData({
                      shareImg:data.data.media_id
                    })
                    wx.showToast({
                      icon: "success",
                      title: "上传成功"
                    });
                  }
                })
            }
          })
    },
    chooseimg: function (e) {
      var that = this;
        wx.chooseImage({
              count: 1,
              sizeType: ['original', 'compressed'],
              sourceType: ['album', 'camera'],
              success (res) {
                console.log(res)
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var imgsrc = res.tempFilePaths;
                that.setData({
                  photo:imgsrc
                }); 
                wx.uploadFile({
                  url: 'https://zb.10soo.cn/wxmini/live/uploads',
                  method:'POST',
                  filePath:imgsrc[0],//图片路径需要字符串而不是数组
                  name:'files',
                  formData: {
                    'user': 'test'
                  },
                  header: {
                    "Content-Type": "multipart/form-data"
                  },
                  success (res) {
                    var data = JSON.parse(res.data)
                    that.setData({
                      coverImg:data.data.media_id
                    })
                    wx.showToast({
                      icon: "success",
                      title: "上传成功"
                    });
                  }
                })
            }
          })
    },
    formSubmit: function(e){
      var that = this;
      var FormData = e.detail.value;
      console.log(FormData)
      wx.request({
        url: 'https://zb.10soo.cn/wxmini/live/create',
        method:"get",
        header: {
          'content-type': 'application/json' // 默认值
        },
        data:FormData,
        success: function(res){
          console.log(res)
        }
      })
    }
})